<template>
  <div class="page-container">
    <div class="page-content">
      <div class="panel-container">
        <div class="panel-content">
          <div class="panel-line space-between">
            <div class="panel-left">
              <el-radio-group v-model="tabActive" style="margin-right: 30px" size="mini" @change="handleTabChange">
                <el-radio-button v-for="item in tabList" :key="item.value" :label="item.value">{{ item.label }}({{ item.num }})</el-radio-button>
              </el-radio-group>
              <el-button class="show-search" type="text" @click="handleShowSearch">
                <span v-if="showSearch"> 收起搜索 </span>
                <span v-else> 展开搜索 </span>
                <i class="el-icon-arrow-down" :class="{ rotate: showSearch }"></i>
              </el-button>
            </div>
            <div class="panel-right">
              <el-button type="primary" @click="handlePublish" size="mini">+ 发布</el-button>
            </div>
          </div>
          <!--搜索-->
          <SearchUiNew ref="SearchUiNew" style="margin-top: 10px" v-show="showSearch" :searchFormArr="searchFormArr" @searchParma="searchParma"></SearchUiNew>
        </div>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <div class="panel-list" style="margin: 10px 0">
          【独享总数：{{ count1.total }}】【推送人数：{{ count1.pub1 }}】【查看人数：{{ count1.view1 }}】【获取联系方式人数：{{ count1.view_phone1 }}】【减少推荐人数：{{ count1.dec1 }}】【查看率：{{ count1.view_percent }}%】【采纳率：{{ count1.accept_percent }}%】【减少推荐率：{{
            count1.dec_percent
          }}%】
        </div>
        <el-table stripe :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading" @sort-change="handleSortChange">
          <el-table-column align="center" fixed="left" header-align="center" show-overflow-tooltip label="ID" prop="number_id" width="80"></el-table-column>
          <el-table-column align="center" fixed="left" header-align="center" show-overflow-tooltip label="信息标题" prop="title" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="详情内容" prop="desc" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="状态" prop="status" width="100">
            <template slot-scope="scope"> {{ getSts(scope.row.sts) }} </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="推送人数" prop="pub_count" width="100" sortable="custom"></el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="查看人数" prop="view_count" width="100" sortable="custom"></el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="获取联系方式人数" prop="view_phone_count" width="140" sortable="custom"></el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="减少推荐人数" prop="dec_send_count" width="100" sortable="custom"></el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="查看率" prop="view_percent" width="100" sortable="custom">
            <template slot-scope="scope"> {{ scope.row.view_percent }}% </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="采纳率" prop="accept_percent" width="100" sortable="custom">
            <template slot-scope="scope"> {{ scope.row.accept_percent }}% </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="减少推荐率" prop="dec_percent" width="100" sortable="custom">
            <template slot-scope="scope"> {{ scope.row.dec_percent }}% </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="流转时长/h" prop="turn_hours"> </el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="已公开" prop="is_open">
            <template slot-scope="scope"> {{ parseInt(scope.row.is_open) === 1 ? '是' : '否' }} </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="信息来源">
            <template slot-scope="scope"> {{ getSource(scope.row.source) }} </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="发布人" prop="pub_man_name"> </el-table-column>
          <el-table-column align="center" header-align="center" show-overflow-tooltip label="发布时间" prop="created_time" width="150"> </el-table-column>
          <af-table-column align="center" fixed="right" header-align="center" show-overflow-tooltip label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleDetail(scope)" class="button-gray">查看</el-button>
              <!-- <template v-if="(scope.row.sts === 1 || scope.row.sts === 4) && scope.row.is_open === 0"> <el-button type="text" @click="handlePublic(scope)">公开</el-button></template> -->
              <template v-if="scope.row.sts === 2">
                <el-button type="text" @click="handleNotDeal(scope)" class="button-danger">未成交</el-button>
                <el-button type="text" @click="handleDeal(scope)">确认成交</el-button>
              </template>
            </template>
          </af-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>

    <!--  确认成交 -->
    <el-dialog title="确认成交" :visible.sync="showDialogDeal">
      <DialogDeal v-if="showDialogDeal" :id="currentId" @close="showDialogDeal = false" @success="handleDealSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import { apiNoShareList, apiNoSharePub, apiNoShareDealConfirm } from '@/api/assetsAlone'
import DialogDeal from './components/DialogDeal.vue'

export default {
  name: 'AloneManagement',
  components: { DialogDeal },
  data() {
    const sourceList = [
      { value: 0, label: '全部' },
      { value: 1, label: 'AI外呼' },
      { value: 2, label: '员工挖掘' },
      { value: 3, label: '社群' },
      { value: 4, label: '新媒体' },
      { value: 5, label: '其他' },
    ]
    const acceptResultList = [
      { value: 0, label: '无操作' },
      { value: 1, label: '采纳' },
      { value: 2, label: '不采纳' },
    ]
    return {
      sourceList,
      acceptResultList,
      currentId: '',
      longOperation: false,
      query: {},
      multipleSelection: [],
      showDialogDeal: false,
      showSearch: false,
      tabActive: '',
      tabList: [
        { value: '', label: '全部', num_name: 'total', num: 0 },
        { value: 1, label: '推送中', num_name: 'w1', num: 0 },
        { value: 4, label: '推送完毕', num_name: 'w4', num: 0 },
        { value: 2, label: '成交待核实', num_name: 'w2', num: 0 },
        { value: 3, label: '已成交', num_name: 'w3', num: 0 },
      ],
      searchFormArr: [
        {
          fieldName: '信息来源:',
          type: 'radioGroup',
          field: 'source',
          width: '100%',
          option: sourceList,
        },
        {
          fieldName: '分类:',
          type: 'cascader',
          field: 'cate',
          props: { value: 'num_id', label: 'name', checkStrictly: true },
          option: [],
        },
        {
          fieldName: '地区:',
          type: 'cascader',
          field: 'zone',
          props: { value: 'code', label: 'name', checkStrictly: true },
          option: [],
        },
        {
          fieldName: '发布人:',
          type: 'select',
          field: 'pub_man_id',
          option: [],
          placeholder: '请选择',
        },
        {
          fieldName: '检索:',
          type: 'input',
          field: 'search',
          placeholder: '请输入检索',
        },
        {
          fieldName: '',
          type: 'select',
          field: 'search_type',
          labelWidth: '0px',
          default: 'title',
          clearable: false,
          option: [
            { value: 'title', label: '按标题' },
            { value: 'desc', label: '按内容' },
            { value: 'user_name', label: '按联系人' },
            { value: 'user_phone', label: '按联系电话' },
            { value: 'company_name', label: '按公司名称' },
          ],
          placeholder: '请选择',
        },

        {
          fieldName: '创建时间:',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择创建时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
      ],

      searchParams: {
        title: '',
        desc: '',
        user_name: '',
        user_phone: '',
        company_name: '',
        key_words: '',
        source: '',
        value: '',
        pub_man_id: '',
        c1: '',
        c2: '',
        sheng_id: '',
        shi_id: '',
        zone_id: '',
        start_time: '',
        end_time: '',
      },
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
      sort: {},
      count1: {},
    }
  },
  activated() {
    this.$refs.table && this.$refs.table.doLayout()
  },
  mounted() {
    this.$bus.$on('assetsAloneListUpdate', this.getDataList)
    this.init()
  },
  destroyed() {
    this.$bus.$off('assetsAloneListUpdate', this.getDataList)
  },
  methods: {
    // 初始化
    init() {
      this.getSelectList()
      this.query = this.$route.query
      const { phone, pub_man_id, start_time, end_time, tab } = this.query
      if (tab !== undefined) {
        this.tabActive = tab
      }
      if (phone) {
        this.showSearch = true
        this.searchParams.user_phone = phone
      }
      if (pub_man_id) {
        this.showSearch = true
        this.searchParams.pub_man_id = pub_man_id
      }
      if (start_time && end_time) {
        this.showSearch = true
        this.searchParams.start_time = start_time
        this.searchParams.end_time = end_time
      }

      // 默认查询当前登录人--又不查了
      // if (Object.keys(this.query).length === 0) {
      //   const currentUserId = localStorage.getItem('feijiu_user_id')
      //   if (currentUserId) {
      //     this.searchParams.pub_man_id = currentUserId
      //   }
      // }

      this.$nextTick(() => {
        const setItemList = []
        if (phone) {
          setItemList.push({ key: 'search', value: phone })
          setItemList.push({ key: 'search_type', value: 'user_phone' })
        }
        if (pub_man_id) {
          setItemList.push({ key: 'pub_man_id', value: pub_man_id })
        }
        if (start_time && end_time) {
          setItemList.push({ key: 'time', value: [start_time, end_time] })
        }
        // 默认查询当前登录人--又不查了
        // if (Object.keys(this.query).length === 0) {
        //   const currentUserId = localStorage.getItem('feijiu_user_id')
        //   setItemList.push({ key: 'pub_man_id', value: currentUserId })
        // }
        if (setItemList.length > 0) {
          this.$refs.SearchUiNew.setItemValue(setItemList)
        } else {
          this.getDataList()
        }
      })
    },
    handleShowSearch() {
      this.showSearch = !this.showSearch
    },
    // 获取下拉数据
    getSelectList() {
      this.$store.dispatch('select/getCateList').then((res) => {
        const formItem = this.searchFormArr.find((item) => item.field === 'cate')
        formItem.option = res
      })

      this.$store.dispatch('select/getCityList').then((res) => {
        const formItem = this.searchFormArr.find((item) => item.field === 'zone')
        formItem.option = res
      })
      this.$store.dispatch('select/getUserList').then((res) => {
        const formItem = this.searchFormArr.find((item) => item.field === 'pub_man_id')
        formItem.option = res
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let info = { pageNumber: this.pageNumber, pageSize: this.pageSize, sts: this.tabActive }
      info = Object.assign(info, this.searchParams)
      // 排序
      if (this.sort.order && this.sort.prop) {
        info[this.sort.prop] = this.sort.order === 'ascending' ? 1 : this.sort.order === 'descending' ? -1 : ''
      }
      apiNoShareList(info)
        .then((res) => {
          const { data, total, others, count1 } = res
          this.count1 = count1
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
          this.tabList.forEach((item) => {
            item.num = others[item.num_name]
          })
          this.longOperation = false
          this.dataList.forEach((item) => {
            if (item.sts < 3 && this.isPubMan(item.pub_man_id)) {
              this.longOperation = true
            }
          })
          this.$refs.table && this.$refs.table.doLayout()
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      let list = ['cate', 'zone', 'search', 'search_type', 'time']
      this.searchFormArr.forEach((item) => {
        if (list.includes(item.field)) return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })

      if (e.cate) {
        this.searchParams.c1 = e.cate[0] || ''
        this.searchParams.c2 = e.cate[1] || ''
      } else {
        this.searchParams.c1 = this.searchParams.c2 = ''
      }
      if (e.zone) {
        this.searchParams.sheng_id = e.zone[0] || ''
        this.searchParams.shi_id = e.zone[1] || ''
        this.searchParams.zone_id = e.zone[2] || ''
      } else {
        this.searchParams.sheng_id = this.searchParams.shi_id = this.searchParams.zone_id = ''
      }
      const formItem1 = this.searchFormArr.find((item) => item.field === 'search_type')
      const option1 = formItem1.option
      option1.forEach((item) => {
        this.searchParams[item.value] = ''
      })
      if (e.search) {
        this.searchParams[e.search_type] = e.search
      }
      if (e.time) {
        this.searchParams.start_time = e.time[0]
        this.searchParams.end_time = e.time[1]
      } else {
        this.searchParams.start_time = this.searchParams.end_time = ''
      }
      this.pageNumber = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 切换tab
    handleTabChange() {
      this.pageNumber = 1
      this.getDataList()
    },
    // 发布
    handlePublish() {
      this.$store.dispatch('permission/routeAuthorization', 'AlonePublish').then((res) => {
        this.$bus.$emit('forceUpdate', 'AlonePublish')
        this.$router.push({
          path: '/assetsAlone/alonePublish',
        })
      })
    },
    // 查看
    handleDetail(scope) {
      this.$store.dispatch('permission/routeAuthorization', 'AloneDetail').then((res) => {
        this.$bus.$emit('forceUpdate', 'AloneDetail')
        this.$router.push({
          path: '/assetsAlone/aloneDetail',
          query: {
            id: scope.row.id,
          },
        })
      })
    },
    // 编辑
    handleEdit(scope) {
      this.$store.dispatch('permission/routeAuthorization', 'AlonePublish').then((res) => {
        this.$bus.$emit('forceUpdate', 'AlonePublish')
        this.$router.push({
          path: '/assetsAlone/alonePublish',
          query: {
            id: scope.row.id,
          },
        })
      })
    },
    // 公开
    handlePublic(scope) {
      window.enterPreventDefault = false
      this.$confirm('确认将本条独享信息公开至Feijiu前台？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          window.enterPreventDefault = true
          apiNoSharePub({ id: scope.row.id }).then(() => {
            this.$message({
              type: 'success',
              message: '操作成功!',
            })
            this.getDataList()
          })
        })
        .catch(() => {
          window.enterPreventDefault = true
        })
    },
    // 未成交
    handleNotDeal(scope) {
      window.enterPreventDefault = false
      this.$confirm('已与处置方核实未成交？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          window.enterPreventDefault = true
          apiNoShareDealConfirm({ id: scope.row.id, sts: 1 }).then(() => {
            this.$message({
              type: 'success',
              message: '操作成功!',
            })
            this.getDataList()
          })
        })
        .catch(() => {
          window.enterPreventDefault = true
        })
    },
    // 确认成交
    handleDeal(scope) {
      this.currentId = scope.row.id
      this.showDialogDeal = true
    },
    // 确认成交成功
    handleDealSuccess() {
      this.showDialogDeal = false
      this.getDataList()
    },
    // 获取状态
    getSts(value) {
      const item = this.tabList.find((item) => item.value === value)
      if (item) {
        return item.label
      } else {
        return ''
      }
    },
    // 获取来源
    getSource(value) {
      const item = this.sourceList.find((item) => item.value === value)
      if (item) {
        return item.label
      } else {
        return ''
      }
    },
    // 获取采纳结果
    getAcceptResult(value) {
      const item = this.acceptResultList.find((item) => item.value === value)
      if (item) {
        return item.label
      } else {
        return ''
      }
    },
    // 是否是发布人
    isPubMan(id) {
      const currentUserId = localStorage.getItem('feijiu_user_id')
      const manId = id
      if (currentUserId === manId) {
        return true
      } else {
        return false
      }
    },
    // 表格排序
    handleSortChange(sort) {
      this.sort = sort
      this.getDataList()
    },
  },
}
</script>
